from utils.base import BaseSerializer
from rest_framework.validators import ValidationError
from apps.tests.models import Case, Step, CaseRunLog, Tag

from rest_framework import serializers


class TagSerializer(BaseSerializer):

    class Meta:
        model = Tag
        fields = '__all__'


class TagNameSerializer(BaseSerializer):

    class Meta:
        model = Tag
        fields = ['name']


class CaseSerializers(BaseSerializer):
    name = serializers.CharField(allow_blank=False, error_messages={'blank': "用例名称不能为空"})
    type = serializers.IntegerField(error_messages={'invalid': '用例类型不能为空'})
    module_name = serializers.CharField(source='module.name', read_only=True)
    plant_name = serializers.CharField(source='module.plant.name', read_only=True)
    type_name = serializers.CharField(source='get_type_display', read_only=True)
    tag_name = TagNameSerializer(many=True, read_only=True, source='tag')

    class Meta:
        model = Case
        fields = '__all__'


class StepSerializers(BaseSerializer):
    module_name = serializers.CharField(source='module.name', read_only=True)
    plant_name = serializers.CharField(source='module.plant.name', read_only=True)

    class Meta:
        model = Step
        fields = '__all__'


class CaseRunLogsSerializers(BaseSerializer):

    result_value = serializers.CharField(source='get_result_display', read_only=True)
    case_type = serializers.CharField(source='case.get_type_display', read_only=True)
    case_module = serializers.CharField(source='case.module.name', read_only=True)
    case_plant = serializers.CharField(source='case.module.plant.name', read_only=True)
    case_name = serializers.CharField(source='case.name', read_only=True)
    tag_name = TagNameSerializer(many=True, read_only=True, source='case.tag')

    class Meta:
        model = CaseRunLog
        fields = '__all__'
